package org.example;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class Test5 {
    //leetcode 94 二叉树的中序遍历

    /**
     * 非递归的方式实现二叉树的中序遍历
     * @param root
     * @return
     */
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> ret = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        if (root == null) return ret;
        while (!stack.empty() || root != null) {
            while (root != null) {
                stack.add(root);
                root = root.left;
            }
            root = stack.pop();
            ret.add(root.val);
            root = root.right;
        }
        return ret;
    }
}
